#include "infodialog.h"
#include "ui_infodialog.h"
#include <QPushButton>

InfoDialog::InfoDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::InfoDialog)
{
    ui->setupUi(this);

    //退出按键文字
    ui->buttonBox->button(QDialogButtonBox::Ok)->setText("确定");
    ui->buttonBox->button(QDialogButtonBox::Cancel)->setText("取消");

    //无标题栏,使圆角会透明
    setWindowFlags(windowFlags() | Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);

    //父窗口灰色遮罩
    widget3 = new QWidget(parent, Qt::FramelessWindowHint);
    widget3->resize(parent->size());
    widget3->setObjectName("widget_parent"); // 设置对象句,相当于css里的id
    widget3->setStyleSheet("#widget_parent {background-color:rgba(160, 160, 160,210);}"); // 设置id对应元素的背景色
    widget3->show();
}

InfoDialog::~InfoDialog()
{
    delete widget3;
    delete ui;
}

void InfoDialog::initWindow(const data_t &data)
{
    extern CDatabase *m_db;
    TypeIndex t;
    StatuIndex s;
    ui->label_WidgetTitle->setText(t.toString(data.Type));
    s.initComboBox(ui->comboBox_statu, data.Statu);

    //显示属性
    ui->lineEdit_title->setText(data.Title);
    ui->textEdit_Note->setText(data.Note);
    ui->dateEdit_stop->setDate(QDateTime::fromString(QString::number(data.StopTime), "yyyyMMddhhmmss").date());
    ui->dateEdit_start->setDate(QDate::fromString(QString::number(data.StartTime / 1000000), "yyyyMMdd"));
    ui->dateEdit_create->setDate(QDateTime::fromString(QString::number(data.CreateTime), "yyyyMMddhhmmss").date());

    //显示子项
    QList<data_t> dl;
    m_db->getChild(data.Id, dl);
    for (int i = 0; i < dl.length(); i++)
    {
        ui->listWidget_c->addItem(dl.at(i).Title);
    }

    //显示父项
    dl.clear();
    m_db->getParent(data.ParentId, dl);
    for (int i = 0; i < dl.length(); i++)
    {
        ui->listWidget_p->addItem(dl.at(i).Title);
    }

    //任务无子项
    if (data.Type > 3)
    {
        ui->tabWidget->removeTab(1);
    }
    else if (data.Type == 3)
    {
        ui->tabWidget->setTabText(1, "子任务");
    }
    else
    {
        ui->tabWidget->setTabText(1, "子需求");
    }
}

void InfoDialog::on_checkBox_del_stateChanged(int arg1)
{
    if (arg1 == 2)
        ui->checkBox_del->setStyleSheet("color:red; font:bold");
    else
        ui->checkBox_del->setStyleSheet("color:black; font:normal");
}

QStringList InfoDialog::getTitles()
{
    QString s;
    QStringList titles;
    QStringList tmp = ui->textEdit_add_c->toPlainText().split("\n");
    for (int i = 0; i < tmp.length(); i++)
    {
        s = tmp.at(i).simplified(); //去除多余的空格
        if (s.length() != 0 && s != " ")
            titles.append(s);
    }

    return titles;
}

void InfoDialog::on_dateEdit_start_userDateChanged(const QDate &date)
{
    QDate stop = ui->dateEdit_stop->date();
    if (date >= stop)
    {
        ui->dateEdit_stop->setDate(date.addDays(+1));
    }
}

void InfoDialog::on_dateEdit_stop_userDateChanged(const QDate &date)
{
    QDate start = ui->dateEdit_start->date();
    if (date <= start)
    {
        ui->dateEdit_start->setDate(date.addDays(-1));
    }
}
